import pymysql
from django.shortcuts import render, redirect
from course.models import major_manage
from course import page

# Create your views here.
def index(request):
    # 分页相关
    current_page = request.GET.get('page', 1)
    page_obj = page.Pager(current_page)

    info = major_manage.objects.all()
    # 尝试做成一个搜索框对应多个属性
    string = request.GET.get('major', '')

    if string.strip() != '':
        # 判断搜索框是否存在值


        if string.isdigit():
            # 判断是否是纯数字,纯数字匹配id
            majors = major_manage.objects.filter(id__contains=string).order_by("-update_time")[
                      page_obj.start:page_obj.end]

            all_item = major_manage.objects.filter(id__contains=string).order_by("-update_time").count()
            pager_str = page_obj.page_str(all_item, '/major/?major=' + string)

            # __contains表示包含。
            return render(request, 'major/index.html', {'majors': majors, 'pager_str': pager_str,'info':info})
        else:
            majors = major_manage.objects.filter(major__contains=string).order_by("-update_time")[
                      page_obj.start:page_obj.end]

            all_item = major_manage.objects.filter(major__contains=string).order_by("-update_time").count()
            pager_str = page_obj.page_str(all_item, '/major/?major=' + string)

            return render(request, 'major/index.html', {'majors': majors, 'pager_str': pager_str,'info':info})
    else:  # 不存在则默认输出所有数据
        majors = major_manage.objects.all().order_by("-update_time")[page_obj.start:page_obj.end]

        all_item = major_manage.objects.all().order_by("-update_time").count()
        # print("数目%r"%all_item)
        pager_str2 = page_obj.page_str2(all_item, '/major/')
        # print(pager_str)
        return render(request, 'major/index.html', {'majors': majors, 'pager_str': pager_str2,'info':info})

def add(request):
    if request.method == 'GET':
        return render(request, 'major/add.html')
    else:
        major = request.POST.get('major', '')
        level = request.POST.get('level', '')
        colleges = request.POST.get('colleges', '')
        course_num = request.POST.get('course_num', '')
        provice = request.POST.get('provice', '')
        status = request.POST.get('status', '')
        sort = request.POST.get('sort','')

        test1 = major_manage(major=major, level=level, colleges=colleges,
                              course_num=course_num, provice=provice, status=status,sort=sort)
        test1.save()
        return redirect('../')

# 专业信息修改处理函数
def edit(request):
    if request.method == 'GET':
        id = request.GET.get("id")
        major = major_manage.objects.get(id=id)

        return render(request, 'major/edit.html', {'major':major})
    else:
        id = request.POST.get('id')
        major = request.POST.get('major', '')
        level = request.POST.get('level', '')
        colleges = request.POST.get('colleges', '')
        course_num = request.POST.get('course_num', '')
        provice = request.POST.get('provice', '')
        status = request.POST.get('status', '')
        sort = request.POST.get('sort','')

        #根据id更新数据库的信息
        major_manage.objects.filter(id=id).update(major=major,level=level,
                                                   colleges=colleges, course_num=course_num, provice=provice, status=status,sort=sort)
        return redirect('../')

def delete(request):
    id = request.GET.get("id")

    test1 = major_manage.objects.get(id=id)
    test1.delete()
    return redirect('../')
